package middleware

import (
	"center-platform/internal/controller/v1/user/service"
	"center-platform/utils"
	"github.com/gin-gonic/gin"
	"net/http"
	"strings"
)

func AuthMiddleware() gin.HandlerFunc {
	return func(c *gin.Context) {
		authHeader := c.Request.Header.Get("Authorization")
		if authHeader == "" {
			c.JSON(http.StatusUnauthorized, gin.H{
				"code": 1,
				"msg":  "请传入token",
			})
			c.Abort()
			return
		}
		//按空格拆分
		parts := strings.SplitN(authHeader, " ", 2)
		if !(len(parts) == 2 && parts[0] == "Bearer") {
			c.JSON(http.StatusOK, gin.H{
				"code": -1,
				"msg":  "请求头中auth格式有误",
			})
			c.Abort()
			return
		}
		//解析token包含的信息
		claims, err := utils.ParseToken(parts[1])
		if err != nil {
			c.JSON(http.StatusOK, gin.H{
				"code": -1,
				"msg":  "无效的Token",
			})
			c.Abort()
			return
		}

		var user service.UserService
		exist := user.InfoById(claims.UserId)
		if exist == nil {
			c.JSON(http.StatusOK, gin.H{
				"code": -1,
				"msg":  "无效的Token",
			})
			c.Abort()
			return
		}
		c.Set("user", utils.Auth{
			UserId: claims.UserId,
			Email:  claims.Email,
		})
		c.Next()
	}
}
